package com.tang.algorithm.swordFingerOffer;

/**
 * @author tlm
 * @Description
 * @date 2020/7/10 11:52 上午
 */
public class findNumbersInSortedArray {
	public static void main(String[] args) {
		int[] res = { 1 };
		System.out.println(search(res, 1));
	}

	public static int search(int[] nums, int target) {
		int left = 0, right = nums.length - 1;
		int res = 0;

		while (left <= right) {
			int mid = (left + right) >>> 1;
			if (nums[mid] == target) {
				res++;
				boolean flag1 = true, flag2 = true;
				int left1 = mid, right1 = mid;
				while (flag1 || flag2) {
					left1--;
					right1++;
					if (left1 >= 0 && nums[left1] == target) {
						res++;
					} else {
						flag1 = false;
					}
					if (right1 <= nums.length - 1 && nums[right1] == target) {
						res++;
					} else {
						flag2 = false;
					}
				}
				return res;
			} else if (nums[mid] < target) {
				left = mid + 1;
			} else if (nums[mid] > target) {
				right = mid - 1;
			}
		}
		return 0;
	}
}
